package com.bdqn.t330.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.t330.entity.Right;
import com.bdqn.t330.entity.RightVO;
import com.bdqn.t330.entity.User;
import com.bdqn.t330.entity.UserInfoVO;
import com.bdqn.t330.mapper.UserMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public List<User> findByUsrNameBiz(String userName) {
        return userMapper.findByUsrName(userName);
    }

    @Override
    public User selectOneUserNameBiz(String userName) {
        QueryWrapper<User> queryWrqpper = new QueryWrapper();
        queryWrqpper.eq("usr_name", userName);
        return userMapper.selectOne(queryWrqpper);
    }

    @Override
    public IPage selectUserPage(Page<User> page, QueryWrapper<User> queryWrapper) {
        return userMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<Right> findRightByUsrName(String usrName) {
        return userMapper.findRightByUsrName(usrName);
    }

    @Override
    public UserInfoVO getByUserNameBiz(String usrName) {
        return userMapper.getByUserName(usrName);
    }

/*    public UserDetails loadUserByUsername(String username) {
        UserInfoVO userInfo = userMapper.getByUserName(username);
        //UserInfo userInfo = userRepository.getUserInfo(username);
        return org.springframework.security.core.userdetails.User.withUsername(userInfo.getUsrName())
                .password(userInfo.getUsrPassword()).authorities(userInfo.getAuthoritys()).build();
    }*/


    public UserDetails loadUserByUsername(String username){
        UserInfoVO userInfo = userMapper.getByUserName(username);
        if(userInfo==null){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        List<RightVO> listUserRight = userMapper.getRightByUserName(username);
        List<String> authorities = new ArrayList<>();
        for (RightVO rightVO : listUserRight) {
            authorities.add(rightVO.getRightUrl());
        }
        String str = String.join(",", authorities);
        List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList(str);
        return org.springframework.security.core.userdetails.User.withUsername(userInfo.getUsrName())
                .password(userInfo.getUsrPassword()).roles(userInfo.getRoleName()).authorities(list).build();
    }
}
